package com.hdy.oa.filter;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;


public class loginCheckFilter implements Filter {
    @Override
    public void doFilter(ServletRequest Request, ServletResponse Response, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) Request;
        HttpServletResponse response = (HttpServletResponse) Response;

        String servletPath = request.getServletPath();
        String contextPath = request.getContextPath();
        HttpSession session = request.getSession();
        Object username = session.getAttribute("username");
        if (    username != null || "/login.jsp".equals(servletPath) ||
                "/index.jsp".equals(servletPath) ||"/dept/login".equals(servletPath)||
                "/dept/relog".equals(servletPath)
            ) {
            filterChain.doFilter(request,response);
        }else {
            response.sendRedirect(contextPath);
        }

    }
}
